package org.lex.utils.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;

public class Collections {
	public static <T> boolean addUniqueElement(Collection<T> c, T e) {
		if (true == c.contains(e))
			return false;
		c.add(e);
		return true;
	}

	/*
	 * �� Iterator��Array ֮���໥ת��
	 */
	public static <T> Iterator<T> toIterator(T... array) {
		if (null == array || 0 == array.length)
			return EmptyIterator.<T> getInstance();
		return new ArrayIterator<T>(array);
	}

	/**
	 * @param array
	 * @param from
	 *            ����
	 * @param to
	 *            ������
	 * @return
	 */
	public static <T> Iterator<T> toIterator(T[] array, int from, int to) {
		if (null == array || 0 == array.length)
			return EmptyIterator.<T> getInstance();
		return new ArrayIterator<T>(array, from, to);
	}

	public static <T> Iterator<T> emptyIterator() {
		return EmptyIterator.<T> getInstance();
	}

	public static <T> Enumeration<T> emptyEnumeration() {
		return EmptyIterator.getInstance();
	}

	/*
	 * Factory methods for creating generic-collections, they are short for
	 * typing :-)
	 */
	public static <T> ArrayList<T> newArrayList() {
		return new ArrayList<T>();
	}

	public static <K, V> Hashtable<K, V> newHashtable() {
		return new Hashtable<K, V>();
	}

	public static <K, V> HashMap<K, V> newHashMap() {
		return new HashMap<K, V>();
	}

	public static <T> HashSet<T> newHashSet() {
		return new HashSet<T>();
	}

	private Collections() {
	}
}
